home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-06 | 5.5 KB | 273 lines | [TEXT/CWIE] |
- #ifndef __DEBUGWRITE__
- #include "DebugWrite.h"
- #endif
- #ifndef __AEPRINT__
- #include "AEPrint.h"
- #endif
- #ifndef __EXCEPTIONS__
- #include "Exceptions.h"
- #endif
- #ifndef __MEMUTILS__
- #include "MemUtils.h"
- #endif
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
- #include <string.h>
- #include <stdio.h>
-
- extern "C" {
-
- pascal void DumpAE(const AEDesc* desc);
-
- }
-
- static const Byte gOpenQuote[] = "\p‘";
- static const Byte gCloseQuote[] = "\p’";
- bool gNeverTrue = false;
-
- static DebugWriteProcPtr gDebugWriteProc;
-
- //------------------------------------------------------------------------------
-
- DebugWriteProcPtr GetDebugWriteProc()
- {
- return gDebugWriteProc;
- }
-
- //------------------------------------------------------------------------------
-
- DebugWriteProcPtr SetDebugWriteProc(DebugWriteProcPtr newProc)
- {
- DebugWriteProcPtr oldProc = gDebugWriteProc;
-
- gDebugWriteProc = newProc;
-
- return oldProc;
- }
-
- //------------------------------------------------------------------------------
-
- void StdDebugWriteProc(const void* xdata, Size size, DebugAction action)
- {
- static Str255 sBuffer = {0};
- static const Byte sNewline[2] = { 1, 0x0d };
-
- Byte* data = (Byte*) xdata;
- long count = sBuffer[0];
-
- while (size > 0)
- {
- long extra = count + size - 255; // Bytes left over after after copy
- long copy = (extra > 0) ? size - extra : size; // Bytes to copy
-
- if (copy > 0) // copy what will fit
- {
- BlockMoveData(data, &sBuffer[count+1], copy);
- sBuffer[0] = count + copy;
- data += copy;
- size -= copy;
- }
-
- if (extra >= 0) // More left...
- {
- SysBreakFunc(sBuffer);
- sBuffer[0] = count = 0;
- }
- }
-
- if (action & kDebugWriteNewline)
- {
- StdDebugWriteProc(&sNewline[1], 1, kDebugWrite);
- }
-
- if (sBuffer[0] && (action & kDebugWriteFlush))
- {
- SysBreakFunc(sBuffer);
- sBuffer[0] = 0;
- }
-
- if (action & kDebugBreak)
- {
- Debugger();
- }
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWritePtr(const void* data, Size size, DebugAction action)
- {
- if (gDebugWriteProc != nil)
- {
- (*gDebugWriteProc)(data, size, action);
- }
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWrite(const unsigned char* str, DebugAction action)
- {
- DebugWritePtr(&str[1], str[0], action);
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWrite(const char* str, DebugAction action)
- {
- DebugWritePtr(str, strlen(str), action);
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWriteErr(long num, DebugAction action)
- {
- Str15 str;
-
- NumToString(num, str);
- DebugWrite(str, action);
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWriteNum(long num, DebugAction action)
- {
- Str15 str;
-
- NumToString(num, str);
- DebugWrite(str, action);
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWriteHex(long num, DebugAction action)
- {
- static const char* hex = "0123456789ABCDEF";
-
- char buf[8];
-
- for (int i = 7; i >= 0; i--, num >>= 4)
- {
- buf[i] = hex[num & 0x0F];
- }
-
- DebugWritePtr(buf, 8, action);
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWriteLn(const unsigned char* str, bool breakToDebugger)
- {
- DebugWrite(str, breakToDebugger ? kDebugWriteLnBreak : kDebugWriteLn);
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWriteType(OSType type, DebugAction action)
- {
- DebugWrite((const StringPtr) gOpenQuote);
- DebugWritePtr(&type, sizeof(type));
- DebugWrite((const StringPtr) gCloseQuote, action);
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWriteAE(const AEDesc* desc, DebugAction action)
- {
- char buffer[1024];
- long stringLength;
-
- char* p = nil;
- // Handle h = nil
- OSErr err = LogIfErr(AEPrintSize(desc, &stringLength));
-
- if (err == noErr)
- {
- if (stringLength > sizeof(buffer))
- {
- p = NewPtr(stringLength);
-
- if (p == nil)
- {
- LogIfErr(MemError());
- }
- }
-
- // WithHandleLocked tmp(desc->dataHandle, true);
-
- err = LogIfErr(AEPrint(desc, p ? p : buffer, p != nil ? stringLength : sizeof(buffer)));
-
- if (err == noErr)
- {
- DebugWrite(p ? p : buffer, action);
- }
-
- if (p != nil)
- {
- DisposePtr(p);
- }
- }
- else if (gNeverTrue) // prevent DumpAE from being dead-stripped
- {
- DumpAE(desc);
- }
- }
-
- //------------------------------------------------------------------------------
-
- pascal void DumpAE(const AEDesc* desc)
- {
- char buffer[1024];
- long stringLength;
-
- char* p = nil;
- OSErr err = LogIfErr(AEPrintSize(desc, &stringLength));
-
- if (err == noErr)
- {
- if (stringLength > sizeof(buffer))
- {
- p = NewPtr(stringLength);
-
- if (p == nil)
- {
- LogIfErr(MemError());
- }
- }
-
- // WithHandleLocked tmp(desc->dataHandle, true);
-
- err = LogIfErr(AEPrint(desc, p ? p : buffer, p != nil ? stringLength : sizeof(buffer)));
-
- if (err == noErr)
- {
- printf(p);
- }
-
- if (p != nil)
- {
- DisposePtr(p);
- }
- }
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWriteAddress(const void* addr, DebugAction action)
- {
- DebugWrite("\p@");
- DebugWriteHex((long) addr, action);
- }
-
- //------------------------------------------------------------------------------
-
- void DebugWriteTypeAtAddress(const char* type, const void* addr, DebugAction action)
- {
- DebugWrite(type);
- DebugWriteAddress(addr, action);
- }
-
- //------------------------------------------------------------------------------
-
-